<?php
namespace shenyewangyue\Jt808;
use shenyewangyue\Jt808\Jt808;

class Jt0100{

/*
 * 消息ID：0x0100。
 * 终端注册消息体数据格式见表7。
 *
 * 表 7 终端注册消息体数据格式
 *  | 起始字节 | 字段 | 数据类型 | 描述及要求 |
 *  |---|---|---|---|
 *  | 0 | 省域ID | WORD | 标示终端安装车辆所在的省域，0 保留，由平台取默认值。省域 ID 采用 GB/T 2260 中规定的行政区划代码六位中前两位。 |
 *  | 2 | 市县域ID | WORD | 标示终端安装车辆所在的市域和县域，0 保留，由平台取默认值。市县域 ID 采用 GB/T 2260 中规定的行政区划代码六位中后四位。 |
 *  | 4 | 制造商ID | BYTE[5] | 5 个字节，终端制造商编码 |
 *  | 9 | 终端型号 | BYTE[20] | 20 个字节，此终端型号由制造商自行定义，位数不足时，后补“0X00”。 |
 *  | 29 | 终端ID | BYTE[7] | 7 个字节，由大写字母和数字组成，此终端 ID 由制造商自行定义，位数不足时，后补“0X00”。 |
 *  | 36 | 车牌颜色 | BYTE | 车牌颜色，按照 JT/T415-2006 的 5.4.12。未上牌时，取值为 0。 |
 *  | 37 | 车辆标识 | STRING | 车牌颜色为 0 时，表示车辆 VIN；否则，表示公安交通管理部门颁发的机动车号牌。 |
*/
    public $mesageBody = [];
    public $jt808;
    public function __construct($data)
    {
        $this->mesageBody = $data;
        $this->jt808 = new Jt808();

    }
    public function getJt0100Data(){
        $mesageBodyArray = [
            'provinceIdArray' => $this->getProvinceIdArray(), //
            'provinceIdNumber' => $this->getProvinceIdNumber(), //
            'cityIdArray' => $this->getCityIdArray(), //
            'cityIdNumber' => $this->getCityIdNumber(), //
            'producersIdArray' => $this->getProducersIdArray(), //
            'producersIdNumber' => $this->getProducersIdNumber(), //
            'modelIdArray' => $this->getModelIdArray(), //
            'modelIdNumber' => $this->getModelIdNumber(), //
            'equipmentIdArray' => $this->getEquipmentIdArray(), //
            'equipmentIdNumber' => $this->getEquipmentIdNumber(), //
            'CarColorArray' => $this->getCarColorArray(), //
            'CarColorNumber' => $this->getCarColorNumber(), //
            'CarLogoArray' => $this->getCarLogoArray(), //
            'CarLogoNumber' => $this->getCarLogoNumber(), //
        ];
        return $mesageBodyArray;
    }
//    获取省域 ID 数组
    public function getProvinceIdArray(){
        $provinceIdArray = array_slice($this->mesageBody,0,2);
        return $provinceIdArray;
    }
//    获取省域 ID 字符串
    public function getProvinceIdNumber(){
        $provinceIdArray = $this->getProvinceIdArray();
        $provinceIdNumber = $this->jt808->ArrayToString($provinceIdArray);
//        $res = [];
//        foreach ($provinceIdArray as $k => $v) {
//            $res[$k] = base_convert($v, 16, 10);
//        }
//        $Jt808 = new Jt808();
//        $provinceIdNumber = $Jt808->bcdToString($res);
//        $provinceIdNumber = str_pad($provinceIdNumber, 6, "0", STR_PAD_LEFT);
//        $provinceIdNumber = substr($provinceIdNumber,0,2);
        // 截取前两位
        return $provinceIdNumber;
    }
//    获取市县域 ID 数组
    public function getCityIdArray(){
        $cityIdArray = array_slice($this->mesageBody,2,2);
        return $cityIdArray;
    }
//    获取市县域 ID 字符串
    public function getCityIdNumber(){
        $cityIdArray = $this->getCityIdArray();
        $cityIdNumber = $this->jt808->ArrayToString($cityIdArray);
//        $res = [];
//        foreach ($cityIdArray as $k => $v) {
//            $res[$k] = base_convert($v, 16, 10);
//        }
//        $Jt808 = new Jt808();
//        $cityIdNumber = $Jt808wy->bcdToString($res);
//        $cityIdNumber = str_pad($cityIdNumber, 6, "0", STR_PAD_LEFT);
//        $cityIdNumber = substr($cityIdNumber,2,4);
        return $cityIdNumber;
    }
//    制造商 ID 数组
    public function getProducersIdArray(){
        $producersIdArray = array_slice($this->mesageBody,4,5);
        return $producersIdArray;
    }
//    制造商 ID 字符串
    public function getProducersIdNumber(){
        $producersIdArray = $this->getProducersIdArray();
        $producersIdNumber = $this->jt808->ArrayToString($producersIdArray);
        $producersIdNumber = pack('H*',$producersIdNumber);
//        $Jt808wy = new Jt808wy();
//        $producersIdNumber = $Jt808wy->bytesArrayToString($producersIdArray);
        return $producersIdNumber;
    }
//    终端型号 数组 //echo "zdId:".json_encode(array_slice($MessageBody,9,20));
    public function getModelIdArray(){
        $modelIdArray = array_slice($this->mesageBody,9,20);
        return $modelIdArray;
    }
//    终端型号 字符串
    public function getModelIdNumber(){
        $modelIdArray = $this->getModelIdArray();
        $modelIdNumber = $this->jt808->ArrayToString($modelIdArray);
//        $Jt808wy = new Jt808wy();
//        $modelIdNumber = $Jt808wy->bytesArrayToString($modelIdArray);
        return $modelIdNumber;
    }
//    终端 ID 数组 //echo "cpId:".json_encode(array_slice($MessageBody,29,1));
    public function getEquipmentIdArray(){
        $equipmentIdArray = array_slice($this->mesageBody,29,7);
        return $equipmentIdArray;
    }
//    终端 ID 字符串
    public function getEquipmentIdNumber(){
        $equipmentIdArray = $this->getEquipmentIdArray();
        $equipmentIdNumber = $this->jt808->ArrayToString($equipmentIdArray);
        $equipmentIdNumber = pack('H*',$equipmentIdNumber);
        return $equipmentIdNumber;
    }
//    车牌颜色 数组 //echo "cbId:".json_encode(array_slice($MessageBody,36,1));
    public function getCarColorArray(){
        $carColorArray = array_slice($this->mesageBody,36,1);
        return $carColorArray;
    }
//    车牌颜色 字符串
    public function getCarColorNumber(){
        $carColorArray = $this->getCarColorArray();
        $carColorNumber = $this->jt808->ArrayToString($carColorArray);
        return $carColorNumber;
    }
//    车牌标识 数组 //echo "cbId:".json_encode(array_slice($MessageBody,36,1));
    public function getCarLogoArray(){
        $carLogoArray = array_slice($this->mesageBody,37);
        return $carLogoArray;
    }
//    车牌标识 字符串
    public function getCarLogoNumber(){
        $carLogoArray = $this->getCarColorArray();
        $carLogoNumber = $this->jt808->ArrayToString($carLogoArray);
        $carLogoNumber = pack('H*', $carLogoNumber);
        $carLogoNumber = iconv('GBK', 'utf-8', $carLogoNumber);
        return $carLogoNumber;
    }

}
?>